﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using TM.QWeibo.SDK.Objects;
using TM.QWeibo.Common;

namespace TM.QWeibo.Dal
{
    /// <summary>
    /// 时间线
    /// </summary>
    public class Timeline : Base
    {
        #region 私有属性
        /// <summary>
        /// API
        /// </summary>
        readonly SDK.API.Timeline _timeLine = new SDK.API.Timeline(OAuthInstance);

        /// <summary>
        /// 时间线回调
        /// </summary>
        private Action<ICollection<SDK.Objects.Tweet>> timelineCallback { get; set; }

        #endregion

        /// <summary>
        /// 实例
        /// </summary>
        public static Timeline Instance {
            get {
                return new Timeline();
            }
        }

        #region 私有方法

        /// <summary>
        /// 获取时间线结束的回调
        /// </summary>
        /// <param name="tweets"></param>
        private void GetTimelineEnd(ResponseData<SDK.Objects.Tweet> tweets)
        {
            if (timelineCallback != null)
            {
                var rs = new List<SDK.Objects.Tweet>();
                if (tweets.Data != null && tweets.Data.Info != null) {
                    rs = tweets.Data.Info;
                    timelineCallback(rs);
                    //Cache Tweets
                    foreach (var tweet in rs)
                    {
                        Cache.SterlingService.DataBase.Save(tweet);
                    }
                }
                //Cache Tweets
                if(tweets.Data!=null && tweets.Data.Users!=null)
                {
                    var users = tweets.Data.Users;
                    foreach (var userInfo in users)
                    {
                        var u = new SDK.Objects.UserInfoBase {Name = userInfo.Name, NickName = userInfo.NickName};
                        Cache.SterlingService.DataBase.Save(u);
                    }
                }

                Cache.SterlingService.DataBase.Flush();
            }
        }

        #endregion

        #region 公共方法
        /// <summary>
        /// 登录帐号的首页时间线
        /// </summary>
        /// <param name="pageflag">分页标识</param>
        /// <param name="pagetime">本页起始时间（第一页 0，继续：根据返回记录时间决定）</param>
        /// <param name="callback">回调</param>
        public void Home(PageFlag pageflag, long pagetime, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            timelineCallback = callback;
            _timeLine.GetHomeTimeline(pageflag, pagetime, Params.PageListCount, GetTimelineEnd);
        }

        /// <summary>
        /// 登录帐号的提及间线
        /// </summary>
        /// <param name="pageflag">分页标识</param>
        /// <param name="pagetime">本页起始时间（第一页 0，继续：根据返回记录时间决定）</param>
        /// <param name="lastid">当前页最后一条记录，用用精确翻页用</param>     
        /// <param name="callback">回调</param>
        public void Mentions(PageFlag pageflag, long pagetime, long lastid, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            timelineCallback = callback;
            _timeLine.GetMentionsTimeline(pageflag, pagetime, Params.PageListCount, lastid, GetTimelineEnd);
        }

        /// <summary>
        /// 登录帐号的特别收听用户时间线
        /// </summary>
        /// <param name="pageflag">分页标识</param>
        /// <param name="pagetime">本页起始时间（第一页 0，继续：根据返回记录时间决定）</param>
        /// <param name="lastid">当前页最后一条记录，用用精确翻页用</param>     
        /// <param name="callback">回调</param>
        public void Special(PageFlag pageflag, long pagetime, long lastid, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            timelineCallback = callback;
            _timeLine.GetSpecialTimeline(pageflag, pagetime, Params.PageListCount, lastid, GetTimelineEnd);
        }

        /// <summary>
        /// 获取指定用户的时间线
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageflag"></param>
        /// <param name="pagetime"></param>
        /// <param name="callback"></param>
        public void GetUserTime(string name,PageFlag pageflag, long pagetime, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            timelineCallback = callback;
            _timeLine.GetUserTimeline(pageflag, pagetime, Params.PageListCount,name, GetTimelineEnd);
        }

        /// <summary>
        /// 收藏的微博
        /// </summary>
        /// <param name="pageflag"></param>
        /// <param name="pagetime"></param>
        /// <param name="lastid"></param>
        /// <param name="callback"></param>
        public void Favorite(PageFlag pageflag, long pagetime, long lastid, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            timelineCallback = callback;
            _timeLine.GetMyFavTimeline(pageflag, pagetime, Params.PageListCount, lastid, GetTimelineEnd);
        }

        #endregion
    }
}
